package de.tum.in.eist.im.client.view;

import com.google.gwt.event.dom.client.*;
import com.google.gwt.user.client.ui.*;

import de.tum.in.eist.im.client.*;
import de.tum.in.eist.im.client.domain.*;

/**
 * Displays the login window.
 * 
 * @author Christian Ivicevic (ga73yis@mytum.de)
 * @since 09-02-2011
 * @version 09-02-2011
 */
public class LoginView implements IView {
	/**
	 * The model to obtain data from.
	 */
	private ClientModel model = null;

	private Label infoLabel;
	private FlexTable loginTable;
	private TextBox usernameBox;
	private PasswordTextBox passwordBox;
	private Button loginButton;

	/**
	 * Initialises the {@link IView} object with the specified
	 * {@link ClientModel} object to obtain data from.
	 * 
	 * @param model
	 *            the {@link ClientModel} to obtain data from.
	 * @return the initialised {@link IView} object which can be passed to the
	 *         {@link ClientView} to be rendered.
	 */
	@Override
	public IView create(ClientModel model) {
		this.model = model;
		return this;
	}

	/**
	 * Renders the current {@link IView} object.
	 */
	@Override
	public void render() {
		this.infoLabel = new Label("Some info to be generated dynamically ...");
		this.infoLabel.getElement().setId("infoLabel");
		this.infoLabel.setVisible(false);
		this.loginTable = new FlexTable();
		this.loginTable.setStyleName("loginForm", true);
		this.usernameBox = new TextBox();
		this.usernameBox.setWidth("180px");
		this.usernameBox.addKeyPressHandler(new KeyPressHandler() {
			@Override
			public void onKeyPress(KeyPressEvent event) {
				if(event.getCharCode() == KeyCodes.KEY_ENTER)
					LoginView.this.loginButton.click();
			}
		});
		this.passwordBox = new PasswordTextBox();
		this.passwordBox.setWidth("180px");
		this.passwordBox.addKeyPressHandler(new KeyPressHandler() {
			@Override
			public void onKeyPress(KeyPressEvent event) {
				if(event.getCharCode() == KeyCodes.KEY_ENTER)
					LoginView.this.loginButton.click();
			}
		});
		this.loginButton = new Button("Login");
		this.loginButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				LoginView.this.loginButton.setEnabled(false);
				LoginView.this.usernameBox.setEnabled(false);
				LoginView.this.passwordBox.setEnabled(false);
				ClientController.get().loginUser(
						new User(LoginView.this.usernameBox.getText(),
								LoginView.this.passwordBox.getText()));
			}
		});
		this.loginTable.setText(0, 0, "Username");
		this.loginTable.setWidget(0, 1, this.usernameBox);
		this.loginTable.setText(1, 0, "Password");
		this.loginTable.setWidget(1, 1, this.passwordBox);
		this.loginTable.setWidget(2, 1, this.loginButton);
		RootPanel.get().add(this.infoLabel);
		RootPanel.get().add(this.loginTable);
		// After a logout this one tries to auto fill your username
		if (this.model.getCurrentUser() != null) {
			this.usernameBox.setText(this.model.getCurrentUser().getUsername());
			this.passwordBox.setFocus(true);
		} else {
			this.usernameBox.setFocus(true);
		}
	}

	/**
	 * Message sent when the {@link ClientModel} changes.
	 */
	@Override
	public void update() {
		// Login does not need to be updated.
	}
}
